package com.spring.schedule_system.controller;

import com.auth0.jwt.exceptions.TokenExpiredException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spring.schedule_system.dao.ClassroomDao;
import com.spring.schedule_system.dao.CourseDao;
import com.spring.schedule_system.dao.TeacherDao;
import com.spring.schedule_system.entity.Classroom;
import com.spring.schedule_system.entity.Course;
import com.spring.schedule_system.entity.Teacher;
import com.spring.schedule_system.entity.User;
import com.spring.schedule_system.utils.JwtToken;
import com.spring.schedule_system.utils.MyUtil;
import com.spring.schedule_system.utils.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@CrossOrigin
@Controller
@RequestMapping("/classroom")
@Api(value = "ClassroomController", description = "教室相关api")
public class ClassroomController {
    Boolean flag;
    MyUtil myUtil = new MyUtil();
    @Autowired
    private ClassroomDao classroomDao;


    /**
     * 添加教室
     * @param classroom_name
     * @param classroom_count
     * @param classroom_status
     * @param Authorization
     * @return
     * @throws IOException
     * @throws TokenExpiredException
     */
    @ApiOperation("添加教室")
    @PostMapping("/web/addClassroom")
    @ResponseBody
    public ResponseEntity addClassroom(@RequestParam(required = true) String classroom_name,
                                     @RequestParam(required = true) Integer classroom_count,
                                     @RequestParam(required = true) Integer classroom_status,
                                     @RequestHeader String Authorization) throws IOException, TokenExpiredException {
        ResponseEntity responseEntity;
        String token = Authorization;
        //判断token是否存在或过期
        if (flag = myUtil.judgeToken(token)){
            responseEntity = ResponseEntity.forbidden();
            responseEntity.putDataValue("msg","token不存在或已过期");
            return responseEntity;
        }
        User user = JwtToken.unsign(token, User.class);
        //用户角色，分微信用户和老师用户，1代表微信用户，2代表老师用户,默认为微信用户
        if (user.getUser_role()!=2){
            responseEntity = ResponseEntity.unauthorized();
            responseEntity.putDataValue("msg","权限错误");
            responseEntity.putDataValue("timestamp",myUtil.getTime());
            return responseEntity;
        }
        //根据user_jobNumber查询数据库是否有该用户存在，不存在则注册，存在则注册失败
        QueryWrapper<Classroom> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("classroom_name", classroom_name);
        Classroom classroom = classroomDao.selectOne(queryWrapper);
        //User userFromBase = userDao.selectById(user_name);
        if (classroom == null){
            responseEntity = ResponseEntity.ok();
            Classroom classroom1 = new Classroom();
            classroom1.setClassroom_count(classroom_count);
            classroom1.setClassroom_name(classroom_name);
            classroom1.setClassroom_status(classroom_status);
            classroomDao.insert(classroom1);
            responseEntity.putDataValue("classroom_id",classroom1.getClassroom_id());
            responseEntity.putDataValue("msg","教室注册成功");
        }else {
            responseEntity = ResponseEntity.badRequest();
            responseEntity.putDataValue("msg","教室注册失败");
        }
        return responseEntity;
    }

    /**
     * 修改教室信息
     * @param classroom_id
     * @param classroom_name
     * @param classroom_count
     * @param classroom_status
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("修改教室信息")
    @PutMapping("/web/updateClassroom")
    @ResponseBody
    public ResponseEntity updateClassroom(@RequestParam(required = true) int classroom_id,
                                        @RequestParam(required = false) String classroom_name,
                                        @RequestParam(required = false) Integer classroom_count,
                                        @RequestParam(required = false) Integer classroom_status,
                                        @RequestHeader String Authorization) throws IOException {
        ResponseEntity responseEntity;
        String token = Authorization;
        if (flag =myUtil.judgeToken(token)) {
            responseEntity = ResponseEntity.forbidden();
            responseEntity.putDataValue("msg", "token不存在或已过期");
            return responseEntity;
        }
        User users = JwtToken.unsign(token, User.class);
        if (users == null) {
            responseEntity = ResponseEntity.notFound();
            responseEntity.putDataValue("msg", "用户不存在");
            return responseEntity;
        } else if (users.getUser_role()!=2) {
            responseEntity = ResponseEntity.unauthorized();
            responseEntity.putDataValue("msg", "没有权限");
            return responseEntity;

        } else {
            responseEntity = ResponseEntity.ok();
            Classroom classroom = classroomDao.selectById(classroom_id);
            classroom.setClassroom_status(classroom_status);
            classroom.setClassroom_name(classroom_name);
            classroom.setClassroom_count(classroom_count);
            classroomDao.updateById(classroom);
            responseEntity.putDataValue("msg", "教室修改成功");
            return responseEntity;
        }
    }

    /**
     * 根据classroom_id查询教师
     * @param classroom_id
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("根据classroom_id查询教师")
    @PutMapping("/web/selectByClassroomId")
    @ResponseBody
    public ResponseEntity selectByClassroomId(@RequestParam(required = true) Integer classroom_id,
                                              @RequestHeader String Authorization) throws IOException {
        ResponseEntity responseEntity;
        String token = Authorization;
        if (flag =myUtil.judgeToken(token)) {
            responseEntity = ResponseEntity.forbidden();
            responseEntity.putDataValue("msg", "token不存在或已过期");
            return responseEntity;
        }
        User users = JwtToken.unsign(token, User.class);
        if (users == null) {
            responseEntity = ResponseEntity.notFound();
            responseEntity.putDataValue("msg", "用户不存在");
            return responseEntity;
        } else if (users.getUser_role()==2) {
            responseEntity=ResponseEntity.ok();
            Classroom classroom = classroomDao.selectById(classroom_id);
            responseEntity.putDataValue("classroom",classroom);
            return responseEntity;
        }
        responseEntity=ResponseEntity.unauthorized();
        responseEntity.putDataValue("msg","没有权限");
        return responseEntity;
    }

    /**
     * 根据id删除教室信息
     * @param classroom_id
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("根据id删除教室信息")
    @PutMapping("/web/deleteClassroom")
    @ResponseBody
    public ResponseEntity deleteClassroom(@RequestParam(required = true) Integer classroom_id,
                                          @RequestHeader String Authorization)throws IOException{
        ResponseEntity responseEntity;
        String token=Authorization;
        if(flag=myUtil.judgeToken(token)){
            responseEntity=ResponseEntity.forbidden();
            responseEntity.putDataValue("msg","token不存在或已过期");
            return responseEntity;
        }
        User users=JwtToken.unsign(token,User.class);
        if(users==null){
            responseEntity=ResponseEntity.notFound();
            responseEntity.putDataValue("msg","用户不存在");
            return responseEntity;
        }else if(users.getUser_role()==2) {
            Classroom classroom = classroomDao.selectById(classroom_id);
            if(classroom!=null){
                classroomDao.deleteById(classroom_id);
                responseEntity=ResponseEntity.ok();
                responseEntity.putDataValue("msg","删除成功");
                return responseEntity;
            }
        }
        responseEntity=ResponseEntity.unauthorized();
        responseEntity.putDataValue("msg","没有权限");
        return responseEntity;
    }
    /**
     * 教室列表
     * @param Authorization
     * @param response
     * @return
     * @throws IOException
     */
    @ApiOperation("教室列表")
    @PostMapping("/web/classroomList")
    @ResponseBody
    public ResponseEntity classroomList(@RequestHeader String Authorization,
                                       HttpServletResponse response) throws IOException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        /*星号表示所有的域都可以接受，*/
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");

        ResponseEntity responseData;
        String token = Authorization;
        //通过token解密出来的user对象
        User user = JwtToken.unsign(token, User.class);
        //判断token是否存在或过期
        if (flag = myUtil.judgeToken(token)){
            responseData = ResponseEntity.forbidden();
            responseData.putDataValue("msg","token不存在或已过期");
            return responseData;
        }
        if (user.getUser_role()!=2){
            responseData = ResponseEntity.badRequest();
            responseData.putDataValue("msg","权限错误");
            responseData.putDataValue("timestamp",myUtil.getTime());
            return responseData;
        }
        QueryWrapper<Classroom> wrapper = new QueryWrapper<>();
        List<Classroom> list = classroomDao.selectList(wrapper);
        responseData = ResponseEntity.ok();
        responseData.putDataValue("classroom",list);
        responseData.putDataValue("msg","成功");
        return responseData;
    }
    /**
     * 统计教室数量
     */
    @ApiOperation("统计教室数量")
    @GetMapping("/web/count")
    @ResponseBody
    public ResponseEntity count() throws IOException {
        ResponseEntity responseData;
        int is_delete = 0;
        QueryWrapper<Classroom> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", is_delete);
        int  count =classroomDao.selectCount(queryWrapper);
        responseData = ResponseEntity.ok();
        responseData.putDataValue("count", count);
        return responseData;
    }
    /**
     * 批量删除教室
     * @param arr
     * @param Authorization
     * @return
     */
    @ApiOperation("批量删除教室")
    @GetMapping("/web/deleteClassroomList")
    @ResponseBody
    public ResponseEntity deleteClassroomList(@RequestParam(required = true) int[] arr,
                                           @RequestHeader String Authorization ) throws  IOException{
        ResponseEntity responseData;
        String token = Authorization;
        //通过token解密出来的user对象
        User user = JwtToken.unsign(token, User.class);
        //判断token是否存在或过期
        if (flag = myUtil.judgeToken(token)){
            responseData = ResponseEntity.forbidden();
            responseData.putDataValue("msg","token不存在或已过期");
            return responseData;
        }
        if (user.getUser_role()!=2){
            responseData = ResponseEntity.badRequest();
            responseData.putDataValue("msg","权限错误");
            responseData.putDataValue("timestamp",myUtil.getTime());
            return responseData;
        }
        for (int classroom_id : arr) {
            Classroom classroom = classroomDao.selectById(classroom_id);
            int is_delete = 1;
            classroom.setIs_delete(is_delete);
            classroomDao.updateById(classroom);
        }
        responseData = ResponseEntity.ok();
        responseData.putDataValue("msg", "批量删除数据成功");
        return responseData;


    }
}
